package com.nineshadow.games.launch;

import cn.stylefeng.guns.modular.system.service.*;
import com.nineshadow.games.constants.CommonConstants;
import com.nineshadow.games.interfaces.IGameUser;
import com.nineshadow.games.netty.socket.EchoServer;
import com.nineshadow.games.netty.websocket.WebSocketServer;
import com.nineshadow.games.redismq.PublishService;
import com.nineshadow.games.util.RedisUtil;
import com.nineshadow.games.util.SpringUtils;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Objects;


@Slf4j
@Component
@DependsOn("springContextHolder")
public class HallServer implements ServletContextListener {

    public static IGameConfigService gameConfigService;

    public static RestTemplate restTemplate;

    public static PublishService publishService;

    public static RedisUtil redisUtil;

    public static IGameUserService gameUserService;

    public static IChangeLogService changeLogService;

    public static IGameLogService gameLogService;

    public static IRoomService roomService;

    /**
     * 掉线操作
     *
     * @param ctx
     */
    public static void offline(ChannelHandlerContext ctx) throws Exception {
        Integer userID = ctx.channel().attr(GameWorld.userId_key).get();
        if (userID != null) {
            IGameUser oldUser = GameWorld.getOnlineUser(userID);
            if (oldUser != null) {
                if (oldUser.getCtx().channel().id().asLongText().equals(ctx.channel().id().asLongText())) {
                    log.info("userId:{},掉线操作", userID);
                    HallServer.gameLogService.quit(userID);
                    //删除redis中用户数据
                    oldUser.offline(ctx);
                }
            }
        }
        GameWorld.getSessionMap().remove(ctx.channel().id().asLongText());
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("webSocket初始化");
        WebSocketServer.run();
        initBean();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("webSocket销毁");
        WebSocketServer.destory();
    }

    public void initBean() {
        gameConfigService = SpringUtils.getBean(IGameConfigService.class);
        restTemplate = SpringUtils.getBean(RestTemplate.class);
        publishService=SpringUtils.getBean(PublishService.class);
        redisUtil=SpringUtils.getBean(RedisUtil.class);
        gameUserService=SpringUtils.getBean(IGameUserService.class);
        changeLogService=SpringUtils.getBean(IChangeLogService.class);
        gameLogService=SpringUtils.getBean(IGameLogService.class);
        roomService=SpringUtils.getBean(IRoomService.class);
    }

}
